package thread;

import java.security.PublicKey;
import java.util.Scanner;

public class ThreadDemoTest {
    public static void main(String[] args) {
        /*//获取线程
        Thread main=Thread.currentThread();
        //获取线程的属性
        //getName()
        String name= main.getName();
        //getId()
        long id=main.getId();
        //getProority()方法 获取线程的优先级
        int priority=main.getPriority();
        //isAlive() //判断线程是否存活
        boolean alive=main.isAlive();
        //isDaemon()//判断线程是否是守护线程
        boolean isDaemon= main.isDaemon();
        //isInterrupted()//判断线程是否被中断
        boolean interrupted=main.isInterrupted();*/


        /*//优先级
        //创建线程
        Thread t1=new Thread(){
            public void run(){
                for (int i = 0; i < 1000; i++){
                    System.out.println(getName()+"正在运行");
                }
            }
        };
        Thread t2=new Thread(){
            public void run(){
                for (int i = 0; i < 1000; i++){
                    System.out.println(getName()+"正在运行");
                }
            }
        };
        Thread t3=new Thread(){
            public void run(){
                for (int i = 0; i < 1000; i++){
                    System.out.println(getName()+"正在运行");
                }
            }
        };
        t1.setPriority(Thread.MAX_PRIORITY);
        t2.setPriority(Thread.MIN_PRIORITY);
        t1.start();
        t2.start();
        t3.start();
        //顺序 0 2 1*/

        //线程阻塞
        /*System.out.println("程序开始");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("程序结束");*/

        //倒计时
        /*System.out.println("倒计时开始");
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入倒计时时间");
        for (int i = scan.nextInt();i>0;i--){
            System.out.println(i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("倒计时结束");*/

        //sleep()方法 阻塞线程
        /*Thread lin=new Thread("A"){
            public void run(){
                System.out.println(getName()+"正在运行");
                try {
                    Thread.sleep(500000);
                } catch (InterruptedException e) {
                    System.out.println(getName()+"被中断");
                }
                System.out.println(getName()+"运行结束");
            }
        };

        Thread huang=new Thread("B"){
            public void run(){
                System.out.println(getName()+"正在运行");
                for (int i = 0; i < 5; i++){
                    System.out.println(getName()+"da da da");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(getName()+"运行结束");
                lin.interrupt();
            }
        };
        lin.start();
        huang.start();*/

        //守护线程
        /*Thread rose=new Thread(){
            public void run(){
                for (int i = 0; i < 5; i++) {
                    System.out.println(getName()+"let me go!!!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {

                    }
                }
                System.out.println("aaaaaaaaa");
                System.out.println("扑通");
            }
        };
        Thread jack =new Thread(){
            public void run(){
                while (true){
                    System.out.println(getName()+"you jump i jump");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {

                    }
                }
            }
        };

        rose.start();
        jack.setDaemon(true);
        jack.start();*/


        //买衣服线程锁
        /*Shops shops=new Shops();
        Thread t1=new Thread(){
            public  void run(){
                shops.buy();
            }
        };


        Thread t2=new Thread(){
            public  void run(){
                shops.buy();
            }
        };
        t2.start();
        t1.start();*/

        //synchronized 在静态方法中的使用,锁对象是类对象,修改成同步块的方式实现
        /*Thread t1=new Thread(){
            public void run(){
                Coo.dosome();
            }
        };
        Thread t2=new Thread(){
            public void run(){
                Coo.dosome();
            }
        };
        t1.start();
        t2.start();*/

        //互斥锁
        /*Coo c=new Coo();
        //new Thread(()->c.methodA()).start();
        new Thread(c::methodA).start();
        new Thread(c::methodB).start();*/



    }
}
/*
class  Shops{

    public  void buy(){
        Thread t=Thread.currentThread();
        try {
            System.out.println(t.getName()+"正在购买");
            Thread.sleep(3000);
            synchronized(this) {
                System.out.println(t.getName() + "试穿衣服");
                Thread.sleep(3000);
            }
            System.out.println(t.getName()+"购买结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}*/
/*class Coo{
    public  static void dosome() {
        synchronized (Coo.class) {
            try {
                Thread t = Thread.currentThread();//获取当前线程
                System.out.println(t.getName() + "正在执行dosome方法");

                Thread.sleep(3000);
                System.out.println(t.getName() + "执行dosome方法结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}*/
/*class Coo{
    public synchronized void methodA(){
        Thread t=Thread.currentThread();
        try {
            System.out.println(t.getName()+"正在执行methodA方法");
            Thread.sleep(5000);
            System.out.println(t.getName()+"执行methodA方法结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public synchronized void methodB(){
        Thread t=Thread.currentThread();
        try {
            System.out.println(t.getName()+"正在执行methodB方法");
            Thread.sleep(5000);
            System.out.println(t.getName()+"执行methodB方法结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}*/
